#from multiprocessing.connection import answer_challenge
from PyQt5 import QtCore, QtGui, QtWidgets
from typing import *
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QFrame, QMessageBox
from frame import Ui_frame
import random

'''
_state = 0：初始状态
_state = 1：猜对了
_state = 2：猜小了
_state = 3：猜大了
_state = 4:大了一点
_state = 5:小了一点
_state = 6:机会耗尽
'''

class myFrame(QFrame):
    def __init__(self,parent:Optional[QFrame] = None) -> None:
        super().__init__(parent)
        self.ui = Ui_frame()
        self.ui.setupUi(self)
        myFrame.init(self)

    def init(self):
        self.ui.elucidation.hide()
        self.ui.front.hide()
        self.ui.to.hide()
        self.ui.back.hide()
        self.ui.sure.hide()
        self.ui.elucidation2.hide()
        self.ui.state.hide()
        self.ui.chance.hide()
        self.ui.range.hide()
        self.ui.residueDegree.hide()
        self.ui.restart.hide()
        self.ui.sure2.hide()
        self.ui.guessNumber.hide()
        self.ui.sure.clicked.connect(self.check)

    #更新游戏信息
    def updateGameInformation(self,_state):
        if(_state == 0):
            global key
            key = random.randint(self.ui.front.value(),self.ui.back.value())
            global chance
            chance = self.ui.chance.value()
            self.ui.state.setText('请输入：')
        elif(_state == 1):
            #font = QtGui.QFont()
            #font.setFamily("微软雅黑")
            self.ui.state.setText('恭喜你，猜对了！正确答案是：{0}'.format(key))
            chance -= 1
        elif(_state == 2):
            self.ui.state.setText('猜小了！')
            chance -= 1
        elif(_state == 3):
            self.ui.state.setText('猜大了！')
            chance -= 1
        elif(_state == 4):
            self.ui.state.setText('大了一点！')
            chance -= 1
        elif(_state == 5):
            self.ui.state.setText('小了一点！')
            chance -= 1
        else:
            #font = QtGui.QFont()
            #font.setFamily("微软雅黑")
            self.ui.state.setText('很遗憾，游戏失败！正确答案是：{0}'.format(key))
            chance -= 1
        self.ui.residueDegree.setText('剩余机会：{0}'.format(chance))
        self.ui.range.setText('范围：{0} ~ {1}'.format(self.ui.front.value(),self.ui.back.value()))

    #切换三级界面
    def switchThreeLevelInterface(self):
        myFrame.updateGameInformation(self,0)
        myFrame.threeLevelInterfaceInit(self)

    #三级界面
    def threeLevelInterfaceInit(self):
        self.ui.elucidation.close()
        self.ui.elucidation2.close()
        self.ui.front.close()
        self.ui.to.close()
        self.ui.back.close()
        self.ui.chance.close()
        self.ui.sure.close()
        self.ui.state.show()
        self.ui.residueDegree.show()
        self.ui.range.show()
        self.ui.guessNumber.show()
        self.ui.sure2.show()
        self.ui.restart.show()
        self.ui.guessNumber.setMinimum(self.ui.front.value())
        self.ui.guessNumber.setMaximum(self.ui.back.value())
        self.ui.guessNumber.clear()
        self.ui.sure2.clicked.connect(self.decide)
    
    #检查
    def check(self):
        if(self.ui.back.value() == 0):
            QMessageBox.warning(self, '警告', '必须键入back！', QMessageBox.Yes)
        elif(self.ui.front.value() >= self.ui.back.value()):
            QMessageBox.warning(self, '警告', '随机数范围不正确，前面的数必须大于后面的数！', QMessageBox.Yes)
        elif(self.ui.back.value() - self.ui.front.value() <= 10):
            QMessageBox.warning(self, '警告', '随机数范围必须大于10！', QMessageBox.Yes)
        elif(self.ui.chance.value() <= 2):
            answer = QMessageBox.question(self, '提示', '机会少于3次，确定要继续吗？', QMessageBox.Yes|QMessageBox.No)
            print(answer)
            if(answer == 16384):
                myFrame.switchThreeLevelInterface(self)
            else:
                return
        else:
            myFrame.switchThreeLevelInterface(self)

    #判断
    def decide(self):
        if(chance != 1):
            #猜对了
            if(self.ui.guessNumber.value() == key):
                myFrame.updateGameInformation(self,1)
            #猜小了
            elif(self.ui.guessNumber.value() < key):
                #小了一点
                if(key - self.ui.guessNumber.value() <= 3):
                    myFrame.updateGameInformation(self,5)
                #猜小了
                else:
                    myFrame.updateGameInformation(self,2)

            #猜大了
            elif(self.ui.guessNumber.value() > key):
                #大了一点
                if(self.ui.guessNumber.value() - key <= 3):
                    myFrame.updateGameInformation(self,4)
                #猜大了
                else:
                    myFrame.updateGameInformation(self,3)
        else:
            #猜对了
            if(self.ui.guessNumber.value() == key):
                myFrame.updateGameInformation(self,1)
            else:
                myFrame.updateGameInformation(self,6)
    '''
    def keyReleaseEvent(self, event):
        if str(event.key()) == '16777220':
            print('按下并释放了Enter键')
    '''
        
        